/*
 * @Author: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @Date: 2025-06-22 19:16:38
 * @LastEditors: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @LastEditTime: 2025-06-24 17:11:24
 * @FilePath: /server/workspace/src/base/config.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include"config.h"
#include<list>
namespace Yizhi{
    ConfigVarbase:: ptr Config:: Lookupbase(const std::string &name){
        auto it=Getdatas().find(name);
        return it->first.empty()?nullptr:it->second;
    }

    void Listallmember(const std::string prefix,YAML::Node &node,std::list<std::pair<std::string,YAML::Node>> &output){
        if(prefix.find_first_not_of("abcdefghikjlmnopqrstuvwxyz._0123456789")!=std::string::npos){
            SERVER_LOG_ERROR(SERVER_LOG_ROOT())<<"vaild input"<<prefix;
            return;

        }
        output.push_back(std::make_pair(prefix,node));

        if(node.IsMap()){
            for(auto it=node.begin();it!=node.end();it++){
                Listallmember(prefix.empty()?it->first.Scalar() : prefix+"."+it->first.Scalar(),it->second,output);
            }
        }

    }
    //从Yaml文件当中获取配置信

    void Config::LoadFromYaml(YAML::Node &node){
        std::list<std::pair<std::string,YAML::Node>> allnodes;
        Listallmember("",node,allnodes);
        

        for(auto it:allnodes){
           const std::string key=it.first;
            if(key.empty()){
                continue;
            }

            ConfigVarbase:: ptr Var=Lookupbase(key);
            if(Var){
                if(it.second.IsScalar()){
                    Var->Fromstring(it.second.Scalar());
                }
                else{
                    std::stringstream ss;
                    ss<<it.second;
                    Var->Fromstring(ss.str());
                }
            }
            
        }
    }
}